package edu.gatech.volt2.drdoommgt.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.border.BevelBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.DefaultListModel;
import javax.swing.JTabbedPane;
import javax.swing.SpringLayout;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.JList;

import edu.gatech.volt2.drdoommgt.domain.Appointment;
import edu.gatech.volt2.drdoommgt.domain.Patient;
import edu.gatech.volt2.drdoommgt.domain.TreatmentRecord;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class PatientHealthHistoryView extends JFrame implements IPatientHealthHistoryView{

	private JPanel contentPane;
	private JLabel city;
	private JLabel street;
	private JLabel gender;
	private JLabel birthday;
	private JLabel phone;
	private JLabel email;
	private JList appointmentList;
	private JLabel patientName;
	private PatientChartPanel statsPane;
	private JButton closeButton;
	private JTable table;
	private DefaultListModel appointmentsModel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PatientHealthHistoryView frame = new PatientHealthHistoryView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public PatientHealthHistoryView() {
		setTitle("Patient Heath History");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 693, 558);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		patientName = new JLabel("PatientName");
		patientName.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		panel.add(patientName);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		JPanel patientInfoPane = new JPanel();
		tabbedPane.addTab("Patient Info", null, patientInfoPane, null);
		SpringLayout sl_patientInfoPane = new SpringLayout();
		patientInfoPane.setLayout(sl_patientInfoPane);
		
		JLabel lblBirthday = new JLabel("Birthday:");
		lblBirthday.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, lblBirthday, 10, SpringLayout.NORTH, patientInfoPane);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, lblBirthday, 10, SpringLayout.WEST, patientInfoPane);
		patientInfoPane.add(lblBirthday);
		
		birthday = new JLabel("[BirthDay]");
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, birthday, 0, SpringLayout.NORTH, lblBirthday);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, birthday, 12, SpringLayout.EAST, lblBirthday);
		patientInfoPane.add(birthday);
		
		JLabel lblNewLabel_1 = new JLabel("Gender:");
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 22, SpringLayout.SOUTH, lblBirthday);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, lblNewLabel_1, 0, SpringLayout.WEST, lblBirthday);
		lblNewLabel_1.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		patientInfoPane.add(lblNewLabel_1);
		
		gender = new JLabel("[Gender]");
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, gender, 0, SpringLayout.WEST, birthday);
		sl_patientInfoPane.putConstraint(SpringLayout.SOUTH, gender, 0, SpringLayout.SOUTH, lblNewLabel_1);
		patientInfoPane.add(gender);
		
		JLabel lblAddress = new JLabel("Address:");
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, lblAddress, 61, SpringLayout.SOUTH, lblBirthday);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, lblAddress, 0, SpringLayout.WEST, lblBirthday);
		lblAddress.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		patientInfoPane.add(lblAddress);
		
		street = new JLabel("[Street]");
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, street, 0, SpringLayout.WEST, birthday);
		sl_patientInfoPane.putConstraint(SpringLayout.SOUTH, street, -334, SpringLayout.SOUTH, patientInfoPane);
		patientInfoPane.add(street);
		
		city = new JLabel("[City]");
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, city, 6, SpringLayout.SOUTH, street);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, city, 0, SpringLayout.WEST, birthday);
		patientInfoPane.add(city);
		
		JLabel lblPhone = new JLabel("Phone:");
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, lblPhone, 0, SpringLayout.WEST, lblBirthday);
		lblPhone.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		patientInfoPane.add(lblPhone);
		
		phone = new JLabel("[Phone]");
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, phone, 0, SpringLayout.NORTH, lblPhone);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, phone, 0, SpringLayout.WEST, birthday);
		phone.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		patientInfoPane.add(phone);
		
		JLabel lblNewLabel = new JLabel("Email:");
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 200, SpringLayout.NORTH, patientInfoPane);
		sl_patientInfoPane.putConstraint(SpringLayout.SOUTH, lblPhone, -20, SpringLayout.NORTH, lblNewLabel);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, lblNewLabel, 0, SpringLayout.WEST, lblBirthday);
		lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		patientInfoPane.add(lblNewLabel);
		
		email = new JLabel("[Email]");
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, email, 0, SpringLayout.NORTH, lblNewLabel);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, email, 0, SpringLayout.WEST, birthday);
		patientInfoPane.add(email);
		
		JSeparator separator = new JSeparator();
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, separator, 0, SpringLayout.NORTH, patientInfoPane);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, separator, 113, SpringLayout.EAST, birthday);
		sl_patientInfoPane.putConstraint(SpringLayout.SOUTH, separator, 450, SpringLayout.NORTH, patientInfoPane);
		separator.setOrientation(SwingConstants.VERTICAL);
		separator.setBackground(Color.GRAY);
		separator.setForeground(Color.WHITE);
		patientInfoPane.add(separator);
		
		JLabel lblUpcomingAppointments = new JLabel("Upcoming Appointments:");
		sl_patientInfoPane.putConstraint(SpringLayout.EAST, separator, -58, SpringLayout.WEST, lblUpcomingAppointments);
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, lblUpcomingAppointments, 0, SpringLayout.NORTH, lblBirthday);
		lblUpcomingAppointments.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		patientInfoPane.add(lblUpcomingAppointments);
		
		appointmentList = new JList();
		sl_patientInfoPane.putConstraint(SpringLayout.NORTH, appointmentList, 6, SpringLayout.SOUTH, lblUpcomingAppointments);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, appointmentList, 322, SpringLayout.WEST, patientInfoPane);
		sl_patientInfoPane.putConstraint(SpringLayout.SOUTH, appointmentList, -130, SpringLayout.SOUTH, patientInfoPane);
		sl_patientInfoPane.putConstraint(SpringLayout.WEST, lblUpcomingAppointments, 0, SpringLayout.WEST, appointmentList);
		sl_patientInfoPane.putConstraint(SpringLayout.EAST, appointmentList, -25, SpringLayout.EAST, patientInfoPane);
		patientInfoPane.add(appointmentList);
		
		JPanel appointmentHistoryPane = new JPanel();
		tabbedPane.addTab("Appointment History", null, appointmentHistoryPane, null);
		appointmentHistoryPane.setLayout(null);
		
		JLabel lblNewLabel_2 = new JLabel("Previous Appointments:");
		lblNewLabel_2.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
		lblNewLabel_2.setBounds(6, 6, 217, 19);
		appointmentHistoryPane.add(lblNewLabel_2);
		
		table = new JTable();
		table.setModel(new MyDefaultTableModel(
			new Object[][] {
				
			},
			new String[] {
				"Date", "Complaint", "Diagnosis"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(90);
		table.getColumnModel().getColumn(1).setPreferredWidth(225);
		table.getColumnModel().getColumn(2).setPreferredWidth(240);
		table.setBounds(16, 37, 625, 362);
		appointmentHistoryPane.add(table);
		
		statsPane = new PatientChartPanel();
		tabbedPane.addTab("Charts", null, statsPane, null);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		closeButton = new JButton("Close");
		panel_1.add(closeButton, BorderLayout.EAST);

	}

	@Override
	public void showSelf() {
		this.setVisible(true);
	}

	@Override
	public void hideSelf() {
		this.setVisible(false);
	}

	@Override
	public void setPatient(Patient p) {
		try{
			this.patientName.setText(p.getName().toString());
			this.gender.setText(p.getInfo().getGender());
			this.birthday.setText(p.getInfo().getBirthday().getTime().toString());
			this.email.setText(p.getEmail());
			this.street.setText(p.getInfo().getAddress().getStreetAddress());
			this.city.setText(p.getInfo().getAddress().getCity()+" "+p.getInfo().getAddress().getState()+" "+p.getInfo().getAddress().getZipCode());
			this.phone.setText(p.getInfo().getPhone());
			this.statsPane.setPatient(p);
			
			
			
			/// List upcoming appointments
			List<Appointment> apList = p.getAppointments();
			appointmentsModel = new DefaultListModel();
			for ( Appointment ap : apList ){
				appointmentsModel.addElement(ap);
			}
			this.appointmentList.setModel(appointmentsModel);
			
			/// Create the list of previous medical history
			List<TreatmentRecord> mh = p.getInfo().getMedHistory();
			DefaultTableModel model = (MyDefaultTableModel) this.table.getModel();
			SimpleDateFormat fmt = new SimpleDateFormat("MM/d/yyyy");
			for ( TreatmentRecord tr : mh ){
				model.addRow(new Object[]{fmt.format(tr.getDate().getTime()), tr.getComplaint(), tr.getDiagnosis()});
			}
			this.table.setModel(model);
			
			
		}catch( NullPointerException e ){
			System.out.println("Patient information incomplete");
		}
	}
	
	@Override
	public JButton getCloseButton(){
		return closeButton;
	}

	@Override
	public void addListener(ActionListener al) {
		closeButton.addActionListener(al);
	}
	
	
}
